package com.glorysoft.wangnianxinpda.activity;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;

import android.os.Bundle;

import com.glorysoft.wangnianxinpda.R;
import com.glorysoft.wangnianxinpda.base.App;
import com.glorysoft.wangnianxinpda.base.BaseActivity;
import com.glorysoft.wangnianxinpda.base.Constant;
import com.glorysoft.wangnianxinpda.databinding.ActivityRecipeDownLoadBinding;
import com.glorysoft.wangnianxinpda.entity.PDALotInfoReturnEntity;
import com.glorysoft.wangnianxinpda.entity.net.ResponseEntity;
import com.glorysoft.wangnianxinpda.utils.MyUtils;
import com.glorysoft.wangnianxinpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

public class RecipeDownLoadActivity extends BaseActivity {

    private ActivityRecipeDownLoadBinding binding;

    private String sLotId = "";
    private String sEqpId = "";
    private PDALotInfoReturnEntity mPDALotInfoReturnEntity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_recipe_down_load);
    }

    @Override
    protected void initData() {
        Bundle extras = getIntent().getExtras();
        sEqpId = extras.getString("EQPID");
        binding.tvEqpNo.setText(sEqpId);
        MyUtils.setEditTextListener(binding.etLot, this, s ->{
            sLotId = s;
            requestData();//获取recipe名称

            //region 测试
//            mPDALotInfoReturnEntity.setLOTID(sLotId);
//            mPDALotInfoReturnEntity.setRECIPENAME("recipe1");
//            binding.setEntity(mPDALotInfoReturnEntity);
            //endregion
        });

//        requestData();//获取recipe名称

        //region 测试
//        mPDALotInfoReturnEntity = new PDALotInfoReturnEntity();
//        mPDALotInfoReturnEntity.setEQPID(sEqpId);
//        binding.setEntity(mPDALotInfoReturnEntity);
        //endregion

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle(getString(R.string.recipeDownload));
        freeUI_titleView.setRightListener(getString(R.string.startDownload), v -> downLoad());
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), binding.llMain);

    }

    //下载
    private void downLoad(){
        if (sLotId.equals("")){
            showToast(getString(R.string.please_scan_lotId), false);
            return;
        }
        PDARecipe();//下载recipe
        //region 测试
//        showMessageDialog("下载成功！", getSupportFragmentManager());
        //endregion
    }

    //region 接口调用
    //获取recipe名称
    private void requestData() {
        FreeUI_EntityCallBack<ResponseEntity<PDALotInfoReturnEntity>> callBack = new FreeUI_DialogEntityCallBack<ResponseEntity<PDALotInfoReturnEntity>>(new TypeToken<ResponseEntity<PDALotInfoReturnEntity>>() {
        }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseEntity<PDALotInfoReturnEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(true, getSupportFragmentManager())) {
                    showToast(response.body().getMsg(), true);

                    mPDALotInfoReturnEntity = response.body().getData();
                    binding.setEntity(mPDALotInfoReturnEntity);

                }
            }

            @Override
            public void onError(Response<ResponseEntity<PDALotInfoReturnEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDALotInfo");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("WORK_NO", Constant.loginEntity.getWORK_NO());
        map.addProperty("EQPID", sEqpId);
        map.addProperty("LotID", sLotId);
        map.addProperty("ISRECIPE", "true");
        String method = "PDALotInfo";
        NetUtils.requestPostNet("PDALotInoRequest", method, this, map, callBack);
    }

    //recipe下载
    private void PDARecipe() {
        FreeUI_EntityCallBack<ResponseEntity<?>> callBack = new FreeUI_DialogEntityCallBack<ResponseEntity<?>>(new TypeToken<ResponseEntity<?>>() {
        }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseEntity<?>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(false, getSupportFragmentManager())) {
                    showMessageDialog(response.body().getMsg(), getSupportFragmentManager());

                }
            }

            @Override
            public void onError(Response<ResponseEntity<?>> response) {
                super.onError(response);
                loadError(response.getException(), "PDARecipe");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("WORK_NO", Constant.loginEntity.getWORK_NO());//工号
        map.addProperty("EQPID", sEqpId);//设备号
        map.addProperty("LOTID", sLotId);//批次号
        map.addProperty("RECIPE_NAME", mPDALotInfoReturnEntity.getRECIPENAME());//recipe名称
        String method = "PDARecipe";
        NetUtils.requestPostNet("PDARecipeRequest", method, this, map, callBack);
    }
    //endregion

    //消息提示框
    private void showMessageDialog(String message, FragmentManager fragmentManager) {
        FreeApi_DialogUtils.getConfirmDialog(
                App.app, "确定", message, this::finish
        ).show(fragmentManager, "DIALOG");

    }
}